//
//  WXLSongsListViewController.m
//  段子手
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLSongsListViewController.h"

@interface WXLSongsListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isHeader;

@end

@implementation WXLSongsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    
    [self dataHandelFromUrl];
    
    [self addFooter];
    [self addHeader];
}



#pragma mark - 创建subView
- (void)createSubView {
    
    // 创建collectionView
    [self createCollectionView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake((kUIScreenWidth - 31) / 2, (kUIScreenWidth - 31) / 2 * 1.2);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kUIScreenWidth, kUIScreenHeight - 64 - 60) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[WXLSongsListCollectionViewCell class] forCellWithReuseIdentifier:@"WXLSongsListCollectionViewCell"];
    
}



- (void)addHeader {
    
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        _isHeader = YES;
        [self dataHandelFromUrl];
    }];
    
}


- (void)addFooter {

    
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        _page++;
        _isHeader = NO;
        [self dataHandelFromUrl];
    }];
    
    
    [footer setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载 ..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据" forState:MJRefreshStateNoMoreData];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:17];
    
    // 设置颜色
    footer.stateLabel.textColor = [UIColor blueColor];
    self.collectionView.mj_footer = footer;
    
}




#pragma mark - collectionView delegate & dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WXLSongsListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WXLSongsListCollectionViewCell" forIndexPath:indexPath];
    
    WXLMusicListModel *model = _dataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WXLMusicListModel *model = _dataArr[indexPath.row];
    WXLSongsListCellViewController *wxlSLCVC = [[WXLSongsListCellViewController alloc] init];
    wxlSLCVC.strID = model._id;
    [self presentViewController:wxlSLCVC animated:YES completion:nil];
}



#pragma mark - 数据请求
- (void)dataHandelFromUrl {
    NSString *urlStr = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?s=s200&size=10&hid=1501420936418474&utdid=VGGJcU4veJsDACPAEYL62k1V&q=tag:最热&net=2&app=ttpod&v=v8.4.0.2016022715&alf=alf700159&tid=0&uid=863360026086235&f=f168&page=%ld&imsi=460019890404407", _page];
    [WxlAPPTools GETWithURL:urlStr par:nil success:^(id responseObject) {
        NSArray *tempArr = [WXLMusicListModel modelHandelWithArray:responseObject[@"data"]];
        
        if (_isHeader && _dataArr.count) {
            [_dataArr removeAllObjects];
        }
        if (tempArr.count) {
            for (WXLMusicListModel *model in tempArr) {
                [self.dataArr addObject:model];
            }
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        
        [_collectionView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray new];
        _page = 1;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
